package org.luxor.commons.redis.component;

/**
 * Redis key 构建工具
 *
 * @author Mr.yan @date 2019/10/9
 */
public class RedisKeys {
    private static final String ZWJ = ":";
    private final StringBuilder rootKeys;

    public RedisKeys(StringBuilder rootKeys) {
        this.rootKeys = rootKeys;
    }

    public RedisKeys append(String key) {
        if (this.rootKeys != null) {
            this.rootKeys.append(ZWJ).append(key);
        }
        return this;
    }

    public String build() {
        if (this.rootKeys == null) {
            return "";
        }
        return this.rootKeys.toString();
    }

    public static RedisKeys builder(String applicationName, String key) {
        StringBuilder root = new StringBuilder("luxor:").append(applicationName).append(ZWJ).append(key);
        return new RedisKeys(root);
    }

    public static RedisKeys builder(String key) {
        StringBuilder root = new StringBuilder("luxor:").append(key);
        return new RedisKeys(root);
    }

    public static String buildConfigKey(String key) {
        return "luxor:config:" + key;
    }

    public static String buildTokenKey(String key) {
        return "luxor:token:" + key;
    }

    public static String buildUserTokenKey(String key) {
        return "luxor:user-token:" + key;
    }

    public static String buildUserKey(String key) {
        return "luxor:user:" + key;
    }

    public static String buildPermissionsKey(String key) {
        return "luxor:permissions:" + key;
    }

    public static String buildOnlineTokensKey() {
        return "luxor:online:token";
    }

    public static String buildOnlineUserKey(String key) {
        return "luxor:online:" + key;
    }

    public static String buildCacheKey(String key) {
        return "luxor:cache:" + key;
    }

    @Override
    public String toString() {
        if (rootKeys == null) {
            return "";
        }
        return rootKeys.toString();
    }
}
